<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Shop extends Adminbase
{
    public function index()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $address = getLWByAddress($data['row']['province'] . $data['row']['city'] . $data['row']['area'] . $data['row']['address']);
            $address = json_decode($address, true);
            if ($address['code'] == 0) {
                $data['row']['longitude'] = $address['data'][0];
                $data['row']['latitude'] = $address['data'][1];
            } else {
                $this->error('输入正确位置');
            }

            //真实城市
//            $city_array = array('北京市', '上海市', '天津市', '重庆市');
//            if (in_array($data['row']['province'], $city_array)){
//                $data['row']['real_city']=$data['row']['province'];
//            }else{
//                $data['row']['real_city']=$data['row']['city'];
//            }

            $data = Db::name('shop')
                ->where('id', $data['id'])
                ->update($data['row']);
            $this->success('操作成功');
        }
        $data = Db::name('shop')->find();
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();
        $city = Db::name('area')->where(['name' => $data['city']])->find();
        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });
        $this->assign([
            'data' => $data,
            'city_list' => $city_list,
            'proviec' => $proviec_list,
            'area_list' => $area_list
        ]);
        return $this->fetch();
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }
}